package tech.treec.smartlight;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.young.mylib.utils.FileUtils;
import com.young.mylib.utils.JsonUtils;

import java.io.File;

import tech.treec.smartlight.entity.ConfigEntity;

/**
 * Created by Young on 2025/3/8.
 */
public class ConfigUtils {

    private static final String TAG = "ConfigUtils";
    public static String cfgFileName = FileUtils.getSettingPath("smartRoom") + "setting.json";

    public static ConfigEntity getConfig() {
        try {
//            String fileName = Constants.AppPath.substring(0, Constants.AppPath.length() - 1);
            //   String fileName = Constants.AppPath.substring(0, Constants.AppPath.length() - 1);
            // 获取外部存储的公共目录（例如，用于下载文件的目录）
            String fileName = FileUtils.getSettingPath("smartRoom");
            Log.i(TAG, "FILENAME " + fileName);
            File file = new File(fileName);
            if (!file.exists()) {
                boolean isOk = file.mkdir();
                Log.i(TAG, "make dir is ok?" + isOk);
                if (isOk) {
                    Log.i(TAG, "Make app path ok!");
                } else {
                    return null;
                }

            }
            file = new File(cfgFileName);
            if (!file.exists()) {
                Log.e(TAG, "Config file no exists: " + cfgFileName);
                ConfigEntity entity = new ConfigEntity();
                String jo = JsonUtils.toJSONString(entity);
                FileUtils.writeFile(cfgFileName, jo,false);
                return entity;
            }

            String str = FileUtils.readFile(file);
            Log.i(TAG, "STR " + str);
            return JsonUtils.toBean(str, ConfigEntity.class);
        } catch (Exception e) {
            return null;
        }
    }
}
